/*
 * @lc app=leetcode.cn id=223 lang=cpp
 *
 * [223] 矩形面积
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution {
    int overlap(int ax1, int ax2, int bx1, int bx2)
    {
        //需要保证 ax1 < ax2, bx1 < bx2
        if(bx2 <= ax2 && bx2 >= ax1)
        {
            return std::abs(bx2 - std::max(bx1, ax1));
        }
        else if(bx1 <= ax2 && bx1 >= ax1)
        {
            return std::abs(std::min(ax2, bx2) - bx1);
        }
        else if(ax1 >= bx1 && ax2 <= bx2)
        {
            return ax2 - ax1;
        }
        return 0;
    }
public:
    int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = abs((ax1 - ax2) * (ay1 - ay2));
        int area2 = abs((bx1 - bx2) * (by1 - by2));
        int area3 = abs(overlap(ax1, ax2, bx1, bx2) * overlap(ay1, ay2, by1, by2));
        return area1 + area2 - area3;
    }
};

// int main()
// {
//     cout << Solution().computeArea(-2, -2, 2, 2, -3, -3, 3, -1) << endl;
//     return 0;
// }

// @lc code=end

